package cn.edu.tsinghua.cic.ccss.qo;

import java.util.Date;

import org.forten.core.dao.rdb.hibernate.QueryInfo;
import org.forten.core.dao.rdb.query.hql.expression.Between;
import org.forten.core.dao.rdb.query.hql.expression.BetweenFields;
import org.forten.core.dao.rdb.query.parameter.Param;
import org.forten.core.dao.rdb.query.parameter.Type;
import org.forten.core.qo.QueryObject;
import org.forten.utils.common.DateUtil;
import org.forten.utils.common.StringUtil;
import org.forten.utils.security.MD5Util;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@Component("msgResultOfChekInQo")
@Scope("prototype")
public class MsgResultOfChekInQo implements QueryObject{
	private String cardNum;
	private String cardNumMD5;
	private String cadreName;
	private String cadreNameMD5;
	private String courseName;
	private String start;
	private String end;
	
	
	@Override
	public void clean() {
		
	}
	@Override
	public void beforeBuildQueryInfo() {
		
	}
	@Override
	public QueryInfo afterBuildQueryInfo(QueryInfo qi) {
		Date currTime = DateUtil.currentTime();
		String currTimeStr = DateUtil.convertDateToString(currTime,
				"yyyy/MM/dd HH:mm");
		Param finalCurrTime = Param.getCommonParam("begin", currTimeStr,
				Type.STRING);
		finalCurrTime.setPrefix("to_date(");
		finalCurrTime.setPostfix(",'yyyy/MM/dd HH24:MI')");
		BetweenFields defaultBetweenTime = BetweenFields.between(
				"startTimeChoose", "teachTime", finalCurrTime);
		qi.addExpressionsToWhere(defaultBetweenTime);

		Between betweenTime = null;
		if (start != null && "".equals(end) && !"".equals(start)) {
			Param startParam = Param.getCommonParam("start", start + "0000",
					Type.STRING);
			Param endParam = Param.getCommonParam("end",
					DateUtil.convertDateToString(DateUtil.currentTime(),
							"yyyyMMdd2359"), Type.STRING);
			startParam.setPrefix("to_date(");
			endParam.setPrefix("to_date(");
			startParam.setPostfix(",'yyyyMMddHH24MI')");
			endParam.setPostfix(",'yyyyMMddHH24MI')");
			betweenTime = Between.between("teachTime", startParam, endParam);

		} else if (start != null && end != null && !"".equals(start)
				&& !"".equals(end)) {
			Param startParam = Param.getCommonParam("start", start + " 00:00",
					Type.STRING);
			Param endParam = Param.getCommonParam("end", end + " 23:59",
					Type.STRING);
			startParam.setPrefix("to_date(");
			endParam.setPrefix("to_date(");
			startParam.setPostfix(",'yyyyMMdd HH24:MI')");
			endParam.setPostfix(",'yyyyMMdd HH24:MI')");
			betweenTime = Between.between("teachTime", startParam, endParam);
		}

		if (betweenTime != null) {
			qi.addExpressionsToWhere(betweenTime);
		}
		return qi;
	}
	public String getStart() {
		return start;
	}
	public void setStart(String start) {
		this.start = start;
	}
	public String getEnd() {
		return end;
	}
	public void setEnd(String end) {
		this.end = end;
	}
	public void setCardNum(String cardNum) {
		if (StringUtil.hasText(cardNum)) {
			this.cardNumMD5 = MD5Util.encrypt(cardNum);
		} else {
			this.cardNumMD5 = null;
		}
		this.cardNum = cardNum;
	}


	public String getCardNumMD5() {
		return cardNumMD5;
	}

	public String getCadreName() {
		return cadreName;
	}

	public void setCadreName(String cadreName) {
		if (StringUtil.hasText(cadreName)) {
			this.cadreNameMD5 = MD5Util.encrypt(cadreName);
		} else {
			this.cadreNameMD5 = null;
		}
		this.cadreName = cadreName;
	}

	public String getCadreNameMD5() {
		return cadreNameMD5;
	}
	
	public String getCourseName() {
		return courseName;
	}
	public void setCourseName(String courseName) {
		this.courseName = courseName;
	}
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((cadreName == null) ? 0 : cadreName.hashCode());
		result = prime * result
				+ ((cadreNameMD5 == null) ? 0 : cadreNameMD5.hashCode());
		result = prime * result + ((cardNum == null) ? 0 : cardNum.hashCode());
		result = prime * result
				+ ((cardNumMD5 == null) ? 0 : cardNumMD5.hashCode());
		result = prime * result
				+ ((courseName == null) ? 0 : courseName.hashCode());
		result = prime * result + ((end == null) ? 0 : end.hashCode());
		result = prime * result + ((start == null) ? 0 : start.hashCode());
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		MsgResultOfChekInQo other = (MsgResultOfChekInQo) obj;
		if (cadreName == null) {
			if (other.cadreName != null)
				return false;
		} else if (!cadreName.equals(other.cadreName))
			return false;
		if (cadreNameMD5 == null) {
			if (other.cadreNameMD5 != null)
				return false;
		} else if (!cadreNameMD5.equals(other.cadreNameMD5))
			return false;
		if (cardNum == null) {
			if (other.cardNum != null)
				return false;
		} else if (!cardNum.equals(other.cardNum))
			return false;
		if (cardNumMD5 == null) {
			if (other.cardNumMD5 != null)
				return false;
		} else if (!cardNumMD5.equals(other.cardNumMD5))
			return false;
		if (courseName == null) {
			if (other.courseName != null)
				return false;
		} else if (!courseName.equals(other.courseName))
			return false;
		if (end == null) {
			if (other.end != null)
				return false;
		} else if (!end.equals(other.end))
			return false;
		if (start == null) {
			if (other.start != null)
				return false;
		} else if (!start.equals(other.start))
			return false;
		return true;
	}
	@Override
	public String toString() {
		return "MsgResultOfChekInQo [cadreName=" + cadreName + ", courseName="
				+ courseName + ", start=" + start + ", end=" + end + "]";
	}
	
	
}
